#pragma once

#include <boostext/typedefs.hpp>
#include <db_schema/TableDL.hpp>

#include "exports.hpp"
#include "UrlReader.hpp"
#include "UrlWriter.hpp"




class CRAWLER_API DatabaseUrlProvider
	: public UrlReader
	, public UrlWriter
{
public:

	typedef boost::shared_ptr<DatabaseUrlProvider>         Ptr;
	typedef boost::shared_ptr<const DatabaseUrlProvider>   const_Ptr;
	typedef QList<QUrl>                                    Urls;

private:

	db::Connection::Ptr      m_connection;
	db::TableDL::const_Ptr   m_table;
	uint64                   m_batchSize;
	uint64                   m_lastId;

	Urls                     m_readCache;

public:

	DatabaseUrlProvider(db::Connection::Ptr connection, db::TableDL::const_Ptr table);
	virtual ~DatabaseUrlProvider();

	virtual boost::optional<QUrl> next();
	virtual void insert(QUrl url);



	/**
	 * Tests if there is a table to operate on.
	 */
	bool hasTable() const;

	/**
	 * Drops and creates a table.
	 */
	void createTable();
};
///////////////////////////////////////////////////////////////////////////////////////////////////
